﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Modules.BankSpecialAccount.Entitys;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.DBContext;
using PmSoft.Repositories;

namespace Modules.BankSpecialAccount.Repositories
{
    [UseDI(ServiceLifetime.Scoped)]
    public class BankSpecialAccountPaymentDetailsRepository : Repository<BankSpecialAccountPaymentDetails, DefaultDbContext>
    {
        private readonly ILogger logger;

        public BankSpecialAccountPaymentDetailsRepository(DefaultDbContext dbContext, IApplicationContext applicationContext, ILogger<BankSpecialAccountPaymentDetailsRepository> logger)
           : base(dbContext, applicationContext)
        {
            this.logger = logger;
        }

        /// <summary>
        /// author:hzj date:2023/11/8 describe:获取支付明细数据集合(分页)
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IPagedList<BankSpecialAccountPaymentDetails> GetProjectSpecialAccountPaymentDetailsList(BankSpecialAccountPaymentDetailsQuery query, int pageIndex, int pageSize)
        {
            Sql sql = Sql.Builder;
            sql.Select("id")
                .From("project_special_account_payment_details");
            if (query.ProjectId.HasValue)
                sql.Where("project_id=@0", query.ProjectId);
            sql.OrderBy("id DESC");
            return GetPagingEntities(pageSize, pageIndex, sql);
        }

        /// <summary>
        /// 是否存在对应的代发明细
        /// </summary>
        /// <param name="htSerial"></param>
        /// <param name="batchNo"></param>
        /// <param name="tradeSerial"></param>
        /// <returns></returns>
        public bool ExistPaymentDetail(string htSerial, string batchNo, string tradeSerial)
        {
            return DbContext.Exists<BankSpecialAccountPaymentDetails>("ht_serial =@0 and batch_no=@1 and trade_serial=@2", htSerial, batchNo, tradeSerial);
        }
    }
}